﻿// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This file is part of CodingEditor.
// Note:	This project is derived from Peter Project
//			(hosted on sourceforge and codeplex)
//
// Copyright (c) 2008-2009, CE Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


namespace SyntaxFileCreator
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    public partial class SyntaxCreator : Form
    {
        #region Fields

        private string TheFile;

        #endregion Fields

        #region Constructors

        public SyntaxCreator()
        {
            InitializeComponent();
            tabControl1.Visible = false;
            tabControl1.Enabled = false;
            toolStripButton2.Enabled = false;
        }

        #endregion Constructors

        #region Methods

        private void Convert()
        {
            StringReader sr = new StringReader(TheFile);
            string nextLine;
            nextLine = sr.ReadLine();
            nextLine = nextLine.Trim();
            string keyword = "<Key word=\"";
            StreamWriter sw = File.CreateText(Path.GetDirectoryName(opFilDialog.FileName) +
                    "\\" + Path.GetFileNameWithoutExtension(opFilDialog.FileName) +
                    ".xshd");
            while (nextLine != null)
            {
                keyword += nextLine + "\"/>";
                sw.WriteLine(keyword);
                nextLine = sr.ReadLine();
                keyword = "<Key word=\"";
            }
            tabControl1.TabPages[1].Text=(Path.GetFileNameWithoutExtension(opFilDialog.FileName) +
                ".xshd"+" CONVERTED");
            sw.Close();
        }

        private void Open()
        {
            tabControl1.SelectedIndex = 1;
            try
            {
                richTextBox2.LoadFile(Path.GetDirectoryName(opFilDialog.FileName) +
                        "\\" + Path.GetFileNameWithoutExtension(opFilDialog.FileName) +
                        ".xshd", RichTextBoxStreamType.PlainText);
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK); }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (opFilDialog.ShowDialog() == DialogResult.OK && opFilDialog.FileName!=null)
            {
                richTextBox1.LoadFile(opFilDialog.FileName, RichTextBoxStreamType.PlainText);
                tabControl1.Visible = true;
                tabControl1.Enabled = true;
                toolStripButton2.Enabled = true;
                tabControl1.TabPages[0].Text = Path.GetFileName(opFilDialog.FileName);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(opFilDialog.FileName, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            TheFile = sr.ReadToEnd();
            Convert();
            sr.Close();
            fs.Close();
            Open();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion Methods
    }
}